const mongoose = require('mongoose')
const Router = require('koa-router')
const fs = require('fs')
let router = new Router()

router.get('/insertAllCategory', async(ctx) => {
  fs.readFile('./data_json/category.json', 'utf8', (err, data) => {
    data = JSON.parse(data)
    let count = 0
    const Category = mongoose.model('Category')
    data.RECORDS.map((value, index) => {
      let newCategory = new Category(value)
      newCategory.save().then(() => {
        count++
        console.log('成功'+count)
      }).catch(err =>{
        console.log('失败')
      })
    })
  })
  ctx.body = '开始导入数据'
})

module.exports = router